package com.asion.qkan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.db.Cartoon;
import com.asion.qkan.db.CartoonDao;
import com.asion.qkan.entry.CategoryEntry;

import com.asion.qkan.jiguangtuisong.ExampleApplication;
import com.asion.qkan.ui.util.ToastUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 * Created by gino on 16-10-15.
 */

public class AllDatasAdapter  extends RecyclerView.Adapter<AllDatasAdapter.ViewHolder>{
    private Context context;
    private List<CategoryEntry.DataBean.TopicsBean> list;
    private OnCLickListener onCLickListener;
    private OnimageClickListener onimageClickListener;
    public AllDatasAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.category_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.entry = list.get(position);
        holder.author.setText(holder.entry.getUser().getNickname());
        holder.image.setImageURI(holder.entry.getCover_image_url());
        holder.title.setText(holder.entry.getTitle());
        holder.rela.setOnClickListener(v -> onCLickListener.onCLickListener(holder.entry));
        holder.watch.setOnClickListener(v -> onimageClickListener.imageviewClick(holder.watch,holder.entry));
    }

    public void setOnClickListener(OnCLickListener onClickListener){
        this.onCLickListener  = onClickListener;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    public void addAll(Collection collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }
    public void removeAll(Collection collection){
        list.removeAll(collection);
        notifyDataSetChanged();
    }
    public void setOnimageClickListener(OnimageClickListener onimageClickListener){
        this.onimageClickListener = onimageClickListener;
    }

    public interface OnCLickListener{
        void onCLickListener(CategoryEntry.DataBean.TopicsBean bean);
    }
    public interface OnimageClickListener{
        void imageviewClick(ImageView i,CategoryEntry.DataBean.TopicsBean bean);
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {

        private  CategoryEntry.DataBean.TopicsBean entry;

        private final SimpleDraweeView image;

        private final TextView author;

        private final TextView like;

        private final TextView title;

        private final TextView recommend;

        private final ImageView watch;
        
        public static boolean isCheck = false;

        private final RelativeLayout rela;
        public ViewHolder(View itemView) {
            super(itemView);
            image = ((SimpleDraweeView) itemView.findViewById(R.id.category_item_imageview));
            author = ((TextView) itemView.findViewById(R.id.category_item_author));
            like = ((TextView) itemView.findViewById(R.id.category_item_like));
            title = ((TextView) itemView.findViewById(R.id.category_item_title));
            recommend = ((TextView) itemView.findViewById(R.id.category_item_recommend));
            watch = (ImageView) itemView.findViewById(R.id.category_item_watch);
            rela = ((RelativeLayout) itemView.findViewById(R.id.category_item_relative));
        }

    }
}
